package admin.controllers;

import helpers.RoleMenuHelper;
import helpers.RolePermissionHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import debugs.RoleDebug;

import models.RoleModel;
import models.StaffModel;

import admin.data.RoleData;
import admin.data.StaffData;

public class PermissionServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String username = (String) request.getParameter("username");
		
		StaffData staffData = new StaffData();
		ArrayList<StaffModel> staffs = staffData.getAll();
		
		RoleData roleData = new RoleData();
		RoleModel role = new RoleModel();
		
		if (username != null) {
			role = roleData.get(username);
			if (role == null) {
				role = new RoleModel();
				role.setUsername(username);
			}
		}
		request.setAttribute("staffs", staffs);
		request.setAttribute("username", username);
		request.setAttribute("role", role);
		try {
			request.getRequestDispatcher("/admin/pages/form_permission.jsp").forward(request, response);
		} catch (ServletException e) {}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {

		String username = (String) request.getParameter("username");
		
		
		String[] permission =  request.getParameterValues("permission");
		List<String> ids = new ArrayList<String>();
		List<String> roles = new ArrayList<String>(Arrays.asList("/admin/dashboard", "/admin/home", "/admin/profile"));
		List<String> menus = new ArrayList<String>();
		
		if (permission != null) {
			for (int i = 0 ; i  < permission.length; i++) {
				ids.add(permission[i]);
			}
		}
		
		if (ids.contains("100")) {
			roles = RolePermissionHelper.getRole100(roles);
			menus = RoleMenuHelper.getMenu100(menus);
		} else {
			if (ids.contains("200")) {
				roles = RolePermissionHelper.getRole200(roles);
				menus = RoleMenuHelper.getMenu200(menus);
			}
			
			if (ids.contains("201")) {
				roles = RolePermissionHelper.getRole201(roles);
				menus = RoleMenuHelper.getMenu201(menus);
			}
			
			if (ids.contains("202")) {
				roles = RolePermissionHelper.getRole202(roles);
				menus = RoleMenuHelper.getMenu202(menus);
			}
			
			if (ids.contains("203")) {
				roles = RolePermissionHelper.getRole203(roles);
				menus = RoleMenuHelper.getMenu203(menus);
			}
			
			if (ids.contains("205")) {
				roles = RolePermissionHelper.getRole205(roles);
				menus = RoleMenuHelper.getMenu205(menus);
			}
			
			if (ids.contains("206")) {
				roles = RolePermissionHelper.getRole206(roles);
				menus = RoleMenuHelper.getMenu206(menus);
			}
			
			if (ids.contains("207")) {
				roles = RolePermissionHelper.getRole207(roles);
				menus = RoleMenuHelper.getMenu207(menus);
			}
			
			if (ids.contains("300")) {
				roles = RolePermissionHelper.getRole300(roles);
				menus = RoleMenuHelper.getMenu300(menus);
			}
			
			if (ids.contains("301")) {
				roles = RolePermissionHelper.getRole301(roles);
				menus = RoleMenuHelper.getMenu301(menus);
			}
			
			if (ids.contains("311")) {
				roles = RolePermissionHelper.getRole311(roles);
				menus = RoleMenuHelper.getMenu311(menus);
			}
			
			if (ids.contains("320")) {
				roles = RolePermissionHelper.getRole320(roles);
				menus = RoleMenuHelper.getMenu320(menus);
			}
			
			if (ids.contains("320")) {
				roles = RolePermissionHelper.getRole320(roles);
				menus = RoleMenuHelper.getMenu320(menus);
			}
			
			if (ids.contains("330")) {
				roles = RolePermissionHelper.getRole330(roles);
				menus = RoleMenuHelper.getMenu330(menus);
			}
			
			if (ids.contains("340")) {
				roles = RolePermissionHelper.getRole340(roles);
				menus = RoleMenuHelper.getMenu340(menus);
			}
			
			if (ids.contains("400")) {
				roles = RolePermissionHelper.getRole400(roles);
				menus = RoleMenuHelper.getMenu400(menus);
			}
			
			if (ids.contains("401")) {
				roles = RolePermissionHelper.getRole401(roles);
				menus = RoleMenuHelper.getMenu401(menus);
			}
			
			if (ids.contains("402")) {
				roles = RolePermissionHelper.getRole402(roles);
				menus = RoleMenuHelper.getMenu402(menus);
			}
			
			if (ids.contains("410")) {
				roles = RolePermissionHelper.getRole410(roles);
				menus = RoleMenuHelper.getMenu410(menus);
			}
			
			if (ids.contains("420")) {
				roles = RolePermissionHelper.getRole420(roles);
				menus = RoleMenuHelper.getMenu420(menus);
			}
			
			if (ids.contains("421")) {
				roles = RolePermissionHelper.getRole421(roles);
				menus = RoleMenuHelper.getMenu421(menus);
			}
			
			if (ids.contains("422")) {
				roles = RolePermissionHelper.getRole422(roles);
				menus = RoleMenuHelper.getMenu422(menus);
			}
			
			if (ids.contains("423")) {
				roles = RolePermissionHelper.getRole423(roles);
				menus = RoleMenuHelper.getMenu423(menus);
			}
			
			if (ids.contains("500")) {
				roles = RolePermissionHelper.getRole500(roles);
				menus = RoleMenuHelper.getMenu500(menus);
			}
			
			if (ids.contains("501")) {
				roles = RolePermissionHelper.getRole501(roles);
				menus = RoleMenuHelper.getMenu501(menus);
			}
			
			if (ids.contains("503")) {
				roles = RolePermissionHelper.getRole503(roles);
				menus = RoleMenuHelper.getMenu503(menus);
			}
			
			if (ids.contains("505")) {
				roles = RolePermissionHelper.getRole505(roles);
				menus = RoleMenuHelper.getMenu505(menus);
			}
			
			if (ids.contains("506")) {
				roles = RolePermissionHelper.getRole506(roles);
				menus = RoleMenuHelper.getMenu506(menus);
			}
			
			if (ids.contains("507")) {
				roles = RolePermissionHelper.getRole507(roles);
				menus = RoleMenuHelper.getMenu507(menus);
			}
			
			if (ids.contains("521")) {
				roles = RolePermissionHelper.getRole521(roles);
				menus = RoleMenuHelper.getMenu521(menus);
			}
			
			if (ids.contains("600")) {
				roles = RolePermissionHelper.getRole600(roles);
				menus = RoleMenuHelper.getMenu600(menus);
			}
			
			if (ids.contains("601")) {
				roles = RolePermissionHelper.getRole601(roles);
				menus = RoleMenuHelper.getMenu601(menus);
			}
			
			if (ids.contains("603")) {
				roles = RolePermissionHelper.getRole603(roles);
				menus = RoleMenuHelper.getMenu603(menus);
			}
			
			if (ids.contains("604")) {
				roles = RolePermissionHelper.getRole604(roles);
				menus = RoleMenuHelper.getMenu604(menus);
			}
			
			if (ids.contains("620")) {
				roles = RolePermissionHelper.getRole620(roles);
				menus = RoleMenuHelper.getMenu620(menus);
			}
			
			if (ids.contains("621")) {
				roles = RolePermissionHelper.getRole621(roles);
				menus = RoleMenuHelper.getMenu621(menus);
			}
			
			if (ids.contains("623")) {
				roles = RolePermissionHelper.getRole623(roles);
				menus = RoleMenuHelper.getMenu623(menus);
			}
			
			if (ids.contains("625")) {
				roles = RolePermissionHelper.getRole625(roles);
				menus = RoleMenuHelper.getMenu625(menus);
			}
			
			if (ids.contains("700")) {
				roles = RolePermissionHelper.getRole700(roles);
				menus = RoleMenuHelper.getMenu700(menus);
			}
			
			if (ids.contains("701")) {
				roles = RolePermissionHelper.getRole701(roles);
				menus = RoleMenuHelper.getMenu701(menus);
			}
			
			if (ids.contains("710")) {
				roles = RolePermissionHelper.getRole710(roles);
				menus = RoleMenuHelper.getMenu710(menus);
			}
			
			if (ids.contains("800")) {
				roles = RolePermissionHelper.getRole800(roles);
				menus = RoleMenuHelper.getMenu800(menus);
			}
			
			if (ids.contains("801")) {
				roles = RolePermissionHelper.getRole801(roles);
				menus = RoleMenuHelper.getMenu801(menus);
			}
			
			if (ids.contains("810")) {
				roles = RolePermissionHelper.getRole810(roles);
				menus = RoleMenuHelper.getMenu810(menus);
			}
			
			if (ids.contains("811")) {
				roles = RolePermissionHelper.getRole811(roles);
				menus = RoleMenuHelper.getMenu811(menus);
			}
			
			if (ids.contains("813")) {
				roles = RolePermissionHelper.getRole813(roles);
				menus = RoleMenuHelper.getMenu813(menus);
			}
			
			if (ids.contains("830")) {
				roles = RolePermissionHelper.getRole830(roles);
				menus = RoleMenuHelper.getMenu830(menus);
			}
			
			if (ids.contains("831")) {
				roles = RolePermissionHelper.getRole831(roles);
				menus = RoleMenuHelper.getMenu831(menus);
			}
			
			if (ids.contains("833")) {
				roles = RolePermissionHelper.getRole833(roles);
				menus = RoleMenuHelper.getMenu833(menus);
			}
			
			if (ids.contains("900")) {
				roles = RolePermissionHelper.getRole900(roles);
				menus = RoleMenuHelper.getMenu900(menus);
			}
			
			if (ids.contains("901")) {
				roles = RolePermissionHelper.getRole901(roles);
				menus = RoleMenuHelper.getMenu901(menus);
			}
			
			if (ids.contains("903")) {
				roles = RolePermissionHelper.getRole903(roles);
				menus = RoleMenuHelper.getMenu903(menus);
			}
			
			if (ids.contains("920")) {
				roles = RolePermissionHelper.getRole920(roles);
				menus = RoleMenuHelper.getMenu920(menus);
			}
			
			if (ids.contains("921")) {
				roles = RolePermissionHelper.getRole921(roles);
				menus = RoleMenuHelper.getMenu921(menus);
			}
			
			if (ids.contains("941")) {
				roles = RolePermissionHelper.getRole941(roles);
				menus = RoleMenuHelper.getMenu941(menus);
			}
			
			if (ids.contains("943")) {
				roles = RolePermissionHelper.getRole943(roles);
				menus = RoleMenuHelper.getMenu943(menus);
			}
			
			if (ids.contains("961")) {
				roles = RolePermissionHelper.getRole961(roles);
				menus = RoleMenuHelper.getMenu961(menus);
			}
			
			if (ids.contains("991")) {
				roles = RolePermissionHelper.getRole991(roles);
				menus = RoleMenuHelper.getMenu991(menus);
			}
		}
		
		RoleData roleData = new RoleData();
		RoleModel role = roleData.get(username);
		
		if (role == null) {
			role = new RoleModel();
			role.setUsername(username);
		}
		
		if (ids.size() > 0) {
			role.setIds(ids);
			role.setPath(roles);
			role.setMenu(menus);
			
			roleData.insert(role);
		}
		RoleDebug.debug(role);
		
		StaffData staffData = new StaffData();
		ArrayList<StaffModel> staffs = staffData.getAll();
		
		request.setAttribute("staffs", staffs);
		request.setAttribute("username", username);
		request.setAttribute("role", role);
		try {
			request.getRequestDispatcher("/admin/pages/form_permission.jsp").forward(request, response);
		} catch (ServletException e) {}
	}
}
